package com.jj.jni;

import org.fusesource.hawtjni.runtime.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.reflect.Field;

/**
 * Copyrights: QidianGroup@JJ
 *
 * @author: LuHan
 * @date: 2018/1/18, 8:57
 */
@JniClass
public class Simple {
    private static Logger logger = LoggerFactory.getLogger(Simple.class);

    private static final Library LIBRARY = new Library("enet", Simple.class);
    static {
        setSysEnv("java.library.path", "D:\\workspace\\act_root\\act\\battleserver\\lib");
        //installDLL("D:\\workspace\\act_root\\act\\battleserver\\lib\\enet.dll");
        //String property = System.getProperty("java.library.path");
        //String workingDir = System.getProperty("user.dir");
        //System.out.println("java.library.path:"+property);
        //System.out.println("user.dir:"+workingDir);
        LIBRARY.load();
    }

    public static native int enet_initialize();

    public static void installDLL(String lualib) {
        File libDir = new File(lualib);
        String libPath = libDir.getAbsolutePath();
        System.load(libPath);
        logger.debug(lualib + " has been loadded!");
    }

    public static void setSysEnv(String prop, String value) {
        System.setProperty(prop, value);
        try {
            Field fieldSysPath;
            fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
            fieldSysPath.setAccessible(true);
            fieldSysPath.set(null, null);
        } catch (Throwable t) {
            logger.error("exception:", t);
        }
    }

    public static void main(String[] args) {

        //setSysEnv("java.library.path", "D:\\workspace\\act_root\\act\\battleserver\\lib");
//        installDLL("D:\\workspace\\act_root\\act\\battleserver\\lib\\enet.dll");

        Simple.enet_initialize();
    }
}
